package org.marverse.serialization

import java.nio.ByteBuffer
import util.{MutableTypedMap, TypedKey}

object SerializationUtil {
  
  object StringS11n_0 extends Inst[String] {
    def ids = Seq(1)
    def classes = Seq(classOf[String])
    
    private val utf8 = java.nio.charset.Charset.forName("UTF-8")
    
    @throws(classOf[java.io.IOException])
    def instantiate(in: ObjectInput, id: ClassRegistry#ID, ctx: MutableTypedMap[TypedKey, Any]) = {
      val len = in.readInt
      val buffer = ByteBuffer.allocate(len)
      in.readBytes(buffer)
      buffer.flip()
      utf8.decode(buffer).toString
    }
    
    @throws(classOf[java.io.IOException])
    def serializeInstance(that: String, out: ObjectOutput) {
      val buffer = utf8.encode(that)
      out.writeInt(buffer.limit)
      out.writeBytes(buffer)
    }
  }
  
  lazy val s11n = Seq[S11nInfo](StringS11n_0)  
  
}
